package com.itheima.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 自动填充功能
 */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        //增加的时候自动添加的时间类
        this.strictInsertFill(metaObject,"createTime", LocalDateTime.class,LocalDateTime.now());
        this.strictUpdateFill(metaObject,"updateTime", LocalDateTime.class,LocalDateTime.now());
        //增加的时候自动添加创建人
        this.strictInsertFill(metaObject,"createUser", Long.class,BaseContext.get());
        this.strictInsertFill(metaObject,"updateUser", Long.class,BaseContext.get());

    }

    /**
     * 更新的时候自动填充
     * @param metaObject
     */

    @Override
    public void updateFill(MetaObject metaObject) {
        //修改时自动创建时间
        this.strictUpdateFill(metaObject,"updateTime", LocalDateTime.class,LocalDateTime.now());
        //修改时自动创建人的信息
        this.strictInsertFill(metaObject,"updateUser", Long.class,BaseContext.get());

    }
}
